package com.cetc.stream;

import com.cetc.lambda.Person;
import org.junit.Test;
import sun.awt.UNIXToolkit;

import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Classname StreamTest01
 * @Description 创建Stream流的四种方式
 * @Date 2023/1/5 15:57
 * @Created by louzhuangxi
 */
public class StreamTest01 {
    List<Person> personList = Arrays.asList(
            new Person("张三", 24),
            new Person("李四", 80),
            new Person("王五", 28),
            new Person("赵六", 42),
            new Person("田七", 28)
    );

    /**
     * 方式一：集合创建stream
     */
    @Test
    public void test01() {
        //返回一个顺序流
        Stream<Person> stream = personList.stream();
        System.out.println(stream);

        //返回一个并行流
        Stream<Person> personStream = personList.parallelStream();
        System.out.println(personStream);
    }

    /**
     * 方式二：数组创建stream
     */
    @Test
    public void test02() {
        int[] arr = new int[]{1, 2, 3, 4};
        IntStream intStream = Arrays.stream(arr);
        System.out.println(intStream);
    }

    /**
     * 方式三：Stream.of()创建流
     */
    @Test
    public void test03() {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
        System.out.println(stream);
    }

    /**
     * 方式四：创建无限流
     */
    @Test
    public void test04() {
        Stream<Integer> stream = Stream.iterate(0, i -> i + 2);
        stream.limit(10).forEach(System.out::println);

        Stream<Double> stream1 = Stream.generate(Math::random);
        stream1.limit(10).forEach(System.out::println);
    }
}
